package com.sm.controller;

import cn.hutool.core.util.ObjectUtil;
import com.sm.model.Collect;
import com.sm.model.HappyRequest;
import com.sm.model.HappySolve;
import com.sm.model.LikeHappyRequest;
import com.sm.service.*;
import com.sm.util.Msg;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/likeRequest")
public class LikeHappyRequestController {

    @Autowired
    private LikeHappyRequestService likeHappyRequestService;
    @Autowired
    private HappyRequestService happyRequestService;
    @Autowired
    private CollectService collectService;
    @Autowired
    private HappySolveService happySolveService;
    /**
     *  幸福问答---对幸福问答/评论进行点赞
     */
    @RequestMapping("/like")
    @ResponseBody
    public Msg like(Integer userId, Integer requestId,Integer type){
        if (userId == null && requestId == null){
            return Msg.fail().add("msg","参数异常");
        }
        LikeHappyRequest likeHappyRequest1 = likeHappyRequestService.selectIsExist(userId, requestId, type);
        if (ObjectUtil.isNotEmpty(likeHappyRequest1)){
            return Msg.fail().add("msg","不能重复点赞");
        }
        LikeHappyRequest likeHappyRequest = new LikeHappyRequest();
        likeHappyRequest.setUserId(userId);
        likeHappyRequest.setCreateTime(new Date());
        likeHappyRequest.setRequestId(requestId);
        likeHappyRequest.setLikeType(type);
        Integer like = likeHappyRequestService.like(likeHappyRequest);
        //更改此条幸福问答的点赞数
        if (type == 0){
            HappyRequest happyRequest = happyRequestService.selectRequestById(requestId);
            HappyRequest record = new HappyRequest();
            record.setLikeNum(happyRequest.getLikeNum()+1);
            record.setRequestId(happyRequest.getRequestId());
            happyRequestService.update(record);
        }else {
            HappySolve happySolve = happySolveService.selectOne(requestId);
            HappySolve record = new HappySolve();
            record.setLikeNum(happySolve.getLikeNum()+1);
            record.setSolveId(happySolve.getSolveId());
            happySolveService.updateLikeNum(record);
        }
        if (like == 1){
            return Msg.success();
        }
        return Msg.fail();
    }


    /**
     * 收藏这条幸福问答
     */
    @RequestMapping("/collect")
    @ResponseBody
    public Msg collect(Integer userId,Integer requestId){
        if(userId == null && requestId == null){
            return Msg.fail().add("msg","参数异常");
        }
        Collect collect1 = collectService.selectCollectBy(userId, 2, requestId);
        if (ObjectUtil.isNotEmpty(collect1)){
            return Msg.fail().add("msg","不能重复收藏");
        }
        Collect collect = new Collect();
        collect.setUserId(userId);
        collect.setCollectType(2);
        collect.setCourseGoodsId(requestId);
        int i = collectService.insertSelective(collect);
        //更改这条幸福问答的收藏数量
        HappyRequest happyRequest = happyRequestService.selectRequestById(requestId);
        HappyRequest record = new HappyRequest();
        record.setCollectNum(happyRequest.getCollectNum()+1);
        record.setRequestId(happyRequest.getRequestId());
        happyRequestService.update(record);
        if (i == 1){
            return Msg.success();
        }
        return Msg.fail();
    }

    /**
     * 幸福问答---取消点赞问题/评论
     */
    @RequestMapping("/cancelLike")
    @ResponseBody
    public Msg cancelLike(Integer userId,Integer requestId,Integer type){
        if (userId == null && requestId == null){
            return Msg.fail().add("msg","参数异常");
        }
        Integer integer = likeHappyRequestService.cancelLike(userId, requestId, type);
        //更改这条幸福问答的点赞个数
        if (type == 0){
            HappyRequest happyRequest = happyRequestService.selectRequestById(requestId);
            HappyRequest record = new HappyRequest();
            record.setLikeNum(happyRequest.getLikeNum()-1);
            record.setRequestId(happyRequest.getRequestId());
            happyRequestService.update(record);
        }else {
            HappySolve happySolve = happySolveService.selectOne(requestId);
            HappySolve record = new HappySolve();
            record.setLikeNum(happySolve.getLikeNum()-1);
            record.setSolveId(happySolve.getSolveId());
            happySolveService.updateLikeNum(record);
        }
        if (integer == 1){
            return Msg.success();
        }
        return Msg.fail();
    }

    /**
     * 幸福问答---取消收藏
     */
    @RequestMapping("/cancelCollect")
    @ResponseBody
    public Msg cancelCollect(Integer userId,Integer requestId){
        if (userId == null && requestId == null){
            return Msg.fail().add("msg","参数异常");
        }
        int i = collectService.cancelCollect(userId, 2, requestId);
        //更改这条幸福问答的收藏数量
        HappyRequest happyRequest = happyRequestService.selectRequestById(requestId);
        HappyRequest record = new HappyRequest();
        record.setCollectNum(happyRequest.getCollectNum()-1);
        record.setRequestId(happyRequest.getRequestId());
        happyRequestService.update(record);
        if (i == 1){
            return Msg.success();
        }
        return Msg.fail();
    }







}
